<template>
    <div class="playlist" @click.self="close" v-show="playListDisplay & this.songList.length !=0">
        <div class="list-wrapper">
            <div class="list-header">
                <div class="title">
                    <i class="iconfont" :class="playMode_class" @click="select_palyMode"></i>
                    <div class="text"></div>
                    <div class="clear" @click="_emptySongList">
                        <i class="iconfont icon-saochu"></i>
                    </div>
                </div>
            </div>

            <div class="list-content" ref="scroll">
                <ul>
                    <li
                        class="item"
                        v-for="(item,index) in songList"
                        :key="item.id"
                        @click="clickSong(item)"
                        :class="{itemCurrent:isActive(index)}"
                    >
                        <i class="current iconfont icon-bofang" v-if="isActive(index)"></i>
                        <span class="text">
                            {{item.name}}-
                            <span
                                class="singer"
                                v-for="singerItem in item.singer"
                                :key="singerItem.id"
                            >{{singerItem.name}}</span>
                        </span>
                        <span class="delete" @click.stop="delete_(index)">
                            <i class="iconfont icon-shanchu"></i>
                        </span>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>
<script>
import {
    mapState,
    mapGetters,
    mapMutations,
    mapActions
} from "@/store/helper/play.js";
import BScroll from "@better-scroll/core";
export default {
    name: "playList",
    data() {
        return {};
    },
    computed: {
        ...mapState({
            songList: state => state.songList,
            playListDisplay: state => state.playListDisplay,
            playMode: state => state.playMode
        }),
        ...mapGetters(["currentIndex"]),
        // 播放模式class
        playMode_class() {
            var _class;
            switch (this.playMode) {
                case "loop":
                    _class = "icon-loop-2";
                    break;
                case "queue":
                    _class = "icon-liebiaoxunhuan";
                    break;
                case "random":
                    _class = "icon-suijixunhuan";
                    break;
                default:
                    break;
            }
            return _class;
        }
    },
    watch: {
        songList: function(val) {
            if (val.length === 0) {
                this.set_playListDisplay(false);
            }
            this.$nextTick(function() {
                this.bs.refresh();
            });
        },
        playListDisplay: function() {
            this.$nextTick(function() {
                this.bs.refresh();
                this.bs.scrollToElement('.itemCurrent')
            });
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        ...mapMutations(["set_playListDisplay", "set_song_list"]),
        ...mapActions([
            "deleteSong",
            "selectSong",
            "emptySongList",
            "select_palyMode"
        ]),
        close() {
            this.set_playListDisplay(false);
        },
        // 切换音乐
        clickSong(song) {
            this.selectSong(song);
        },
        // 当前播放样式
        isActive(index) {
            return this.currentIndex === index ? true : false;
        },
        // 删除列表中的音乐
        delete_(index) {
            this.deleteSong(index);
        },
        // 清空列表中的音乐
        _emptySongList() {
            this.emptySongList();
        },
        init() {
            this.$nextTick(function() {
                this.bs = new BScroll(this.$refs.scroll, {
                    scrollY: true,
                    click: true,
                    probeType: 3 // listening scroll hook
                });
            });
        }
    }
};
</script>

<style lang="less" scoped>
.playlist {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 2000;
    background-color: rgba(0, 0, 0, 0.3);
}
.playlist.list-fade-enter-active,
.playlist.list-fade-leave-active {
    -webkit-transition: opacity 0.3s;
    transition: opacity 0.3s;
}
.playlist.list-fade-enter-active .list-wrapper,
.playlist.list-fade-leave-active .list-wrapper {
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
}
.playlist.list-fade-enter,
.playlist.list-fade-leave-to {
    opacity: 0;
}
.playlist.list-fade-enter .list-wrapper,
.playlist.list-fade-leave-to .list-wrapper {
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0);
}
.playlist .list-wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    border-radius: 8px;
    background-color: #f2f3f4;
}
.playlist .list-wrapper .list-header {
    position: relative;
    padding: 20px 30px 10px 20px;
}
.playlist .list-wrapper .list-header .title {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
}
.playlist .list-wrapper .list-header .title .iconfont {
    margin-right: 10px;
    font-size: 20px;
    color: #757575;
}
.playlist .list-wrapper .list-header .title .text {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    font-size: 14px;
    color: #2e3030;
}
.playlist .list-wrapper .list-header .title .text .count {
    position: relative;
    top: 1px;
}
.playlist .list-wrapper .list-header .title .clear {
    position: relative;
}
.playlist .list-wrapper .list-header .title .clear:before {
    content: "";
    position: absolute;
    top: -10px;
    left: -10px;
    right: -10px;
    bottom: -10px;
}
.playlist .list-wrapper .list-header .title .clear .icon-clear {
    font-size: 14px;
    color: #757575;
}
.playlist .list-wrapper .list-content {
    max-height: 240px;
    overflow: hidden;
}
.playlist .list-wrapper .list-content .item {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 40px;
    padding: 0 30px 0 20px;
    overflow: hidden;
}
.playlist .list-wrapper .list-content .item.list-enter-active,
.playlist .list-wrapper .list-content .item.list-leave-active {
    -webkit-transition: all 0.1s;
    transition: all 0.1s;
}
.playlist .list-wrapper .list-content .item.list-enter,
.playlist .list-wrapper .list-content .item.list-leave-to {
    height: 0;
}
.playlist .list-wrapper .list-content .item .current {
    color: #d44439;
    margin-right: 5px;
}
.playlist .list-wrapper .list-content .item .text {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    line-height: 20px;
    font-size: 14px;
    color: #2e3030;
    .singer {
        font-size: 12px;
        color: #737373;
    }
    .singer + .singer {
        margin-left: 5px;
    }
}
.playlist .list-wrapper .list-content .item .delete {
    position: relative;
    font-size: 11px;
    color: #d44439;
}
.playlist .list-wrapper .list-content .item .delete:before {
    content: "";
    position: absolute;
    top: -10px;
    left: -10px;
    right: -10px;
    bottom: -10px;
}
.playlist .list-wrapper .list-close {
    text-align: center;
    line-height: 50px;
    background: #f2f3f4;
    font-size: 16px;
    color: #2e3030;
}
</style>